sqlNameStr = ""
sqlValueStr = ""
objName = "p_info"
treeObjName = "info"
treeName = "info_tree"
setInfostr = ""
setTreeInfoStr = ""

function trim (s) return (string.gsub(s, "^%s*(.-)%s*$", "%1")) end

function Split(szFullString, szSeparator)
	local nFindStartIndex = 1
	local nSplitIndex = 1
	local nSplitArray = {}

	while true do
		local nFindLastIndex = string.find(szFullString, szSeparator, nFindStartIndex)
		if not nFindLastIndex then
			nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, string.len(szFullString))
			break
		end

		nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, nFindLastIndex - 1)
		nFindStartIndex = nFindLastIndex + string.len(szSeparator)
		nSplitIndex = nSplitIndex + 1
	end

	return nSplitArray

end

function GetValInfo(line)
	local i, j = string.find(line, "//")
	if i == 1 then
		return nil, nil
	end

	print(line)
	local valType = string.gsub(line, "^([^%s]+)%s+([^%s]+);$", "%1")
	local valValue = string.gsub(line, "^([^%s]+)%s+([^%s^;]+);$", "%2")

	return valType, valValue
end

function ProcessLine(line)
	local valType, valValue = GetValInfo(line)
	if nil == valType then
		return
	end

	local valueStr
	local setValueStr

	if valType == "int" then
		valueStr = "lexical_cast<string>(" .. objName .. "->" .. valValue .. ") + \"','\" +\n"
		setValueStr = "lexical_cast<int>(str_list[i++].c_str());"
	elseif valType == "string" then
		valueStr = objName .. "->" .. valValue .. " + \"','\" +\n"
		setValueStr = "str_list[i++].c_str();"
	else
		print("wrong type: (" .. valType .. "),(" .. valValue .. ")")
		return;
	end

	sqlNameStr = sqlNameStr .. valValue .. ",\n"
	sqlValueStr = sqlValueStr .. valueStr

	setInfostr = setInfostr .. objName .. "->" .. valValue .. " = " .. setValueStr .. "\n"
	setTreeInfoStr = setTreeInfoStr .. treeName .. ".put(\"" .. valValue .. "\", " .. treeObjName .. "." .. valValue .. ");\n"
end

function main()

	local rfile = io.open("test.txt", "r");
	local wfile = io.open("result.txt", "w+");

	local hasStruct = false
	local hasBegin = false
	local line

	sqlNameStr = "sql_str = \"("
	sqlValueStr = " values('\" + "

	for line in rfile:lines() do

		if false == hasStruct then
			local i, j = string.find(trim(line), "struct")

			if i ~= nil then
				hasStruct = true;
			end;
		elseif false == hasBegin then
			if trim(line) == "{" then
				hasBegin = true;
			end;
		elseif trim(line) == "};" then
			hasStruct = false;
			hasBegin = false;
		else
			ProcessLine(trim(line))
		end;
	end

	sqlNameStr = string.sub(sqlNameStr, 1, string.len(sqlNameStr) - 2)
	sqlNameStr = sqlNameStr .. ")"
	sqlValueStr = string.sub(sqlValueStr, 1, string.len(sqlValueStr) - 6);
	sqlValueStr = sqlValueStr .. ");\";"

	local sqlStr = sqlNameStr .. sqlValueStr .. "\n\n\n" .. setInfostr .. "\n\n\n" .. setTreeInfoStr
	print(sqlStr)
	wfile:write(sqlStr)

	rfile:close();
	wfile:close();

end

main()
